package jjn.round1;

/**
 * @author Jjn
 * @since 2022/8/17 23:34
 */
public class LeetCode50_PowxN {

    public double myPow(double x, int n) {
        return myPower(x, n);
    }

    public double myPower(double x, long n) {
        if (n < 0) {
            return 1 / calcPower(x, -n);
        }
        return calcPower(x, n);
    }

    private double calcPower(double x, long n) {
        if (Double.valueOf(x).equals(0.0)) {
            return 0;
        }
        if (n == 0) {
            return 1;
        }
        double power = calcPower(x, n / 2);
        if (n % 2 == 1) {
            return power * power * x;
        } else {
            return power * power;
        }
    }
}
